package cn.laiguowei.May3.MD5Signature.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import cn.laiguowei.May3.MD5Signature.utils.AesSignatureAndMD5Utils;
import cn.laiguowei.May3.MD5Signature.utils.AesSignatureAndMD5Utils.TimeExpire;

/**
* @author laiguowei E-mail:1276293854@qq.com   2021年4月30日 下午4:02:35
*/

@Component
public class InterfaceInvokeInterceptor implements HandlerInterceptor{
	
	public final String INTERFACE_INOVKE_LIMITED = "接口访问受限";

	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
			throws Exception {
		String securityBody = request.getParameter("securityBody");
		String md5securyt = request.getParameter("md5securyt");
		Long time = null;
		if(request.getParameter("time")!=null){
			time = Long.parseLong(request.getParameter("time"));
		}
		if((securityBody!=null && securityBody!="")
				&& (md5securyt!=null && md5securyt!="")
					&& (time!=null)) {
			//过期就是true
			boolean isexpire = new  AesSignatureAndMD5Utils().new TimeExpire().build(TimeExpire.SECONDS, 20L).expire(time);
			if(isexpire) {
				response.setContentType("text/json;charset=UTF-8");
				response.getWriter().println(INTERFACE_INOVKE_LIMITED);
				return false;
			}
			return true;
		}else {
			return true;
		}
	}

	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		
	}

	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		
	}

	
}


 